package Hxw3DEngine.utils{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.system.System;
	import flash.text.TextField;
	import flash.utils.Timer;

	public class FPS extends Sprite{
		private var fpsTxt:TextField;
		private var countTxt:TextField;
		private var trianglesTxt:TextField;
		private var drawCallsTxt:TextField;
		private var memoryTxt:TextField;
		private var count:int=0
		
		private static var _insdance:FPS;
		
		public static function get insdance():FPS{
			return _insdance ||= new FPS(new Single);
		}
		
		public function FPS(single:Single){
			init()
		}
		private function init():void{
			x = y = 5;
			//fps
			fpsTxt = creatTextField();
			addChild(fpsTxt)
			//count
			countTxt = creatTextField();
			addChild(countTxt);
			//triangles
			trianglesTxt = creatTextField();
			addChild(trianglesTxt);
			//drawCall
			drawCallsTxt = creatTextField();
			addChild(drawCallsTxt);
			//Memory
			memoryTxt = creatTextField();
			addChild(memoryTxt);
			//计时器
			var myTimer:Timer = new Timer(1000);
			myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
			this.addEventListener(Event.ENTER_FRAME,countHandler)
			myTimer.start();
			timerHandler(null);
		}
		
		//fps
		private function timerHandler(event:TimerEvent):void{
			fpsTxt.text="帧频	:"+count + "帧"
			count=0;
		}
		private function countHandler(event:Event):void{
			count++
			showMemory();
		}
		
		//内存
		private function showMemory():void{
			memoryTxt.text = "内存	:" + Number(System.totalMemory/1024/1024).toFixed(2) + "M";
		}
		
		//渲染信息
		private static var 	currentCount:int;
		private static var  currentTriangle:int;
		private static var 	currentDrawCalls:int;
		public static function showRenderInfo(objNum:int,triangleNum:int,drawCalls:int):void{
			//渲染gameobject数
			if(currentCount != objNum){
				insdance.countTxt.text = "渲染	:" + objNum + "个";
				currentCount = objNum;
			}
			//总面数
			if(currentTriangle != triangleNum){
				insdance.trianglesTxt.text = "面数	:" + triangleNum + "个";
				currentTriangle = triangleNum;
			}
			//drawCalls
			//总面数
			if(currentDrawCalls != drawCalls){
				insdance.drawCallsTxt.text = "calls	:" + drawCalls + "个";
				currentDrawCalls = drawCalls;
			}
		}
		
		//创建文本
		private var i:int = 0;
		public function creatTextField():TextField{
			var txt:TextField = new TextField();
			txt.mouseEnabled = false;
			txt.textColor=0xffffff;
			txt.y = i*20;
			txt.x = 5
			txt.filters = [new GlowFilter(0x000000,1,2,2,10,1)];
				
			i++;
			
			this.graphics.clear();
			this.graphics.beginFill(0x00ff00,.5);
			this.graphics.drawRect(0,0,100,i*20+10);
			this.graphics.endFill();
			
			return txt;
		}
		
	}
	
}

class Single{
	
}